package com.sensePlusVR.utils;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.RequestParams;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.net.NetCallBack;
import com.sensePlusVR.net.RequestUtils;



/**
 * 版本工具
 * @author Charles
 *
 */
@SuppressWarnings("deprecation")
public class VersionUtil {

	private static final String TAG = VersionUtil.class.getSimpleName();
	
	//进率
	private static final long[] bits= new long[]{1000000,10000,100,1};
	
	/**
	 * 获取版本号
	 * @return
	 * 
	 */
	public static String getVersionName(Context context){
		
		PackageManager packageManager = context.getPackageManager();
		
		String packageName = context.getPackageName();
		
		PackageInfo packInfo = null;
		try {
			packInfo = packageManager.getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return packInfo==null?"":packInfo.versionName;
					
	}
	
	public static String getVersionNameOnlyNum(Context context){
		String version = getVersionName(context);
		if(version.length() > 1){
			int index = version.indexOf("v");
			if(index != -1){
				version = version.substring(index+1);
			}
		}
		return version;
	}
	
	
	public interface CheckVersion
	{
		public void onVersionCallback(AppVersionInfo appVersionInfo);
		
	}
	
	public static void checkoutVersion(Context context,final CheckVersion checkVersion){
		
		MyLog.d(TAG, "-----checkoutVersion");
		
		RequestParams params = new RequestParams();
		
		params.put(Constant.MOBILE, Constant.ANDROID_PHONE);

		MyLog.d(TAG, "checkoutVersion url : " + ConstantURL.VERSION_UPDATE);
		
		RequestUtils.clientGet(context, ConstantURL.VERSION_UPDATE, params,new NetCallBack(){
			
			
			@Override
			public void onMySuccess(int statusCode, Header[] aheader,
					JSONObject jsonobject) {
				
				super.onMySuccess(statusCode, aheader, jsonobject);
				
				
				MyLog.d(TAG, "checkoutVersion statusCode:"+statusCode+" json:"+jsonobject);
				AppVersionInfo appVersionInfo = null;
				
				if(statusCode==Constant.HTTP_200&&jsonobject!=null)
				{

					appVersionInfo =  JSON.parseObject(jsonobject.toString(), AppVersionInfo.class);
					
				}
				
				if(checkVersion!=null){
					
					checkVersion.onVersionCallback(appVersionInfo);
				}
			}
			

			@Override
			public void onMyFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onMyFailure(statusCode, headers, throwable, errorResponse);
				
				MyLog.d(TAG, "checkoutVersion onMyFailure:");
				if(checkVersion!=null){
					
					checkVersion.onVersionCallback(null);
					
				}
			}
			
			
		
			
		});
	}
	
	
	
	/**
	 * 安装apk
	 * @param path
	 */
	public static void installApk(Context context,String filePath){
		
		Intent intent = new Intent(Intent.ACTION_VIEW);
		
		intent.setDataAndType(Uri.parse("file://"+filePath), "application/vnd.android.package-archive");
		
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
		
		context.startActivity(intent);
	}
	
	/**
	 * 检查是否更新
	 * @param newVersion
	 * @param curVersion
	 * @return
	 */
	public static boolean canVersionUpdate(String newVersion,String curVersion){
		
		newVersion = newVersion.substring(1);
		curVersion = curVersion.substring(1);
	
		MyLog.d(TAG, "newVersion="+newVersion+" curVersion="+curVersion);
		
		String[] newVals = newVersion.split("\\.");
		
		String[] curVals = curVersion.split("\\.");
		
		MyLog.d(TAG, "newVals="+newVals+" newVals len="+newVals.length);
		
		long newCode = 0;
		long curCode = 0;
		
		for(int i = 0;i < newVals.length;i++){
			
			if(newVals[i] != null && newVals[i].length() > 0){
				
				newCode += Long.parseLong(newVals[i])*bits[i];
				
			}
			
			if(curVals[i] != null && curVals[i].length() > 0){
				
				curCode += Long.parseLong(curVals[i])*bits[i];	
				
			}
			
		}
		
		MyLog.d(TAG, "newCode="+newCode+" curCode="+curCode);
	
		
		return (newCode>curCode);
	}
}
